1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
| using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using CodeMonkey.Utils;
public class Window_Graph : MonoBehaviour {
public static Window_Graph instance;
[SerializeField] private Sprite dotSprite; private RectTransform graphContainer; private RectTransform labelTemplateX; private RectTransform labelTemplateY; private RectTransform dashContainer; private RectTransform dashTemplateX; private RectTransform dashTemplateY; private List<GameObject> gameObjectList; private List<IGraphVisualObject> graphVisualObjectList; private GameObject tooltipGameObject; private List<RectTransform> yLabelList;
public List<int> valueList; private IGraphVisual graphVisual; private int maxVisibleValueAmount; private Func<int, string> getAxisLabelX; private Func<float, string> getAxisLabelY; private float xSize; [SerializeField] private bool startYScaleAtZero = true;
private void Awake() { instance = this; graphContainer = transform.Find("graphContainer").GetComponent<RectTransform>(); labelTemplateX = graphContainer.Find("labelTemplateX").GetComponent<RectTransform>(); labelTemplateY = graphContainer.Find("labelTemplateY").GetComponent<RectTransform>(); dashContainer = graphContainer.Find("dashContainer").GetComponent<RectTransform>(); dashTemplateX = dashContainer.Find("dashTemplateX").GetComponent<RectTransform>(); dashTemplateY = dashContainer.Find("dashTemplateY").GetComponent<RectTransform>(); tooltipGameObject = graphContainer.Find("tooltip").gameObject;
gameObjectList = new List<GameObject>(); yLabelList = new List<RectTransform>(); graphVisualObjectList = new List<IGraphVisualObject>(); IGraphVisual lineGraphVisual = new LineGraphVisual(graphContainer, dotSprite, Color.green, new Color(1, 1, 1, .5f)); IGraphVisual barChartVisual = new BarChartVisual(graphContainer, Color.white, .8f);
transform.Find("barChartBtn").GetComponent<Button_UI>().ClickFunc = () => { SetGraphVisual(barChartVisual); }; transform.Find("lineGraphBtn").GetComponent<Button_UI>().ClickFunc = () => { SetGraphVisual(lineGraphVisual); }; HideTooltip();
List<int> valueList = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; ShowGraph(valueList, barChartVisual, -1, (int _i) => "" + (_i), (float _f) => "" + Mathf.RoundToInt(_f));
}
public static void ShowTooltip_Static(string tooltipText, Vector2 anchoredPosition) { instance.ShowTooltip(tooltipText, anchoredPosition); }
private void ShowTooltip(string tooltipText, Vector2 anchoredPosition) { tooltipGameObject.SetActive(true);
tooltipGameObject.GetComponent<RectTransform>().anchoredPosition = anchoredPosition;
Text tooltipUIText = tooltipGameObject.transform.Find("text").GetComponent<Text>(); tooltipUIText.text = tooltipText;
float textPaddingSize = 4f; Vector2 backgroundSize = new Vector2( tooltipUIText.preferredWidth + textPaddingSize * 2f, tooltipUIText.preferredHeight + textPaddingSize * 2f );
tooltipGameObject.transform.Find("background").GetComponent<RectTransform>().sizeDelta = backgroundSize;
tooltipGameObject.transform.SetAsLastSibling(); }
public static void HideTooltip_Static() { instance.HideTooltip(); }
private void HideTooltip() { tooltipGameObject.SetActive(false); }
private void SetGetAxisLabelX(Func<int, string> getAxisLabelX) { ShowGraph(this.valueList, this.graphVisual, this.maxVisibleValueAmount, getAxisLabelX, this.getAxisLabelY); }
private void SetGetAxisLabelY(Func<float, string> getAxisLabelY) { ShowGraph(this.valueList, this.graphVisual, this.maxVisibleValueAmount, this.getAxisLabelX, getAxisLabelY); }
private void IncreaseVisibleAmount() { ShowGraph(this.valueList, this.graphVisual, this.maxVisibleValueAmount + 1, this.getAxisLabelX, this.getAxisLabelY); }
private void DecreaseVisibleAmount() { ShowGraph(this.valueList, this.graphVisual, this.maxVisibleValueAmount - 1, this.getAxisLabelX, this.getAxisLabelY); }
private void SetGraphVisual(IGraphVisual graphVisual) { ShowGraph(this.valueList, graphVisual, this.maxVisibleValueAmount, this.getAxisLabelX, this.getAxisLabelY); }
private void ShowGraph(List<int> valueList, IGraphVisual graphVisual, int maxVisibleValueAmount = -1, Func<int, string> getAxisLabelX = null, Func<float, string> getAxisLabelY = null) { this.valueList = valueList; this.graphVisual = graphVisual; this.getAxisLabelX = getAxisLabelX; this.getAxisLabelY = getAxisLabelY;
if (maxVisibleValueAmount <= 0) { maxVisibleValueAmount = valueList.Count; } if (maxVisibleValueAmount > valueList.Count) { maxVisibleValueAmount = valueList.Count; }
this.maxVisibleValueAmount = maxVisibleValueAmount;
if (getAxisLabelX == null) { getAxisLabelX = delegate (int _i) { return _i.ToString(); }; } if (getAxisLabelY == null) { getAxisLabelY = delegate (float _f) { return Mathf.RoundToInt(_f).ToString(); }; }
foreach (GameObject gameObject in gameObjectList) { Destroy(gameObject); } gameObjectList.Clear(); yLabelList.Clear();
foreach (IGraphVisualObject graphVisualObject in graphVisualObjectList) { graphVisualObject.CleanUp(); } graphVisualObjectList.Clear();
graphVisual.CleanUp(); float graphWidth = graphContainer.sizeDelta.x; float graphHeight = graphContainer.sizeDelta.y;
float yMinimum, yMaximum; CalculateYScale(out yMinimum, out yMaximum);
xSize = graphWidth / (maxVisibleValueAmount + 1);
int xIndex = 0; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++) { float xPosition = xSize + xIndex * xSize; float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;
string tooltipText = getAxisLabelY(valueList[i]); IGraphVisualObject graphVisualObject = graphVisual.CreateGraphVisualObject(new Vector2(xPosition, yPosition), xSize, tooltipText); graphVisualObjectList.Add(graphVisualObject);
RectTransform labelX = Instantiate(labelTemplateX); labelX.SetParent(graphContainer, false); labelX.gameObject.SetActive(true); labelX.anchoredPosition = new Vector2(xPosition, -7f); labelX.GetComponent<Text>().text = getAxisLabelX(i); gameObjectList.Add(labelX.gameObject); RectTransform dashX = Instantiate(dashTemplateX); dashX.SetParent(dashContainer, false); dashX.gameObject.SetActive(true); dashX.anchoredPosition = new Vector2(xPosition, -3f); gameObjectList.Add(dashX.gameObject);
xIndex++; }
int separatorCount = 10; for (int i = 0; i <= separatorCount; i++) { RectTransform labelY = Instantiate(labelTemplateY); labelY.SetParent(graphContainer, false); labelY.gameObject.SetActive(true); float normalizedValue = i * 1f / separatorCount; labelY.anchoredPosition = new Vector2(-7f, normalizedValue * graphHeight); labelY.GetComponent<Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); yLabelList.Add(labelY); gameObjectList.Add(labelY.gameObject);
RectTransform dashY = Instantiate(dashTemplateY); dashY.SetParent(dashContainer, false); dashY.gameObject.SetActive(true); dashY.anchoredPosition = new Vector2(-4f, normalizedValue * graphHeight); gameObjectList.Add(dashY.gameObject); } }
public void UpdateValue(int index, int value) { float yMinimumBefore, yMaximumBefore; CalculateYScale(out yMinimumBefore, out yMaximumBefore);
valueList[index] = value;
float graphWidth = graphContainer.sizeDelta.x; float graphHeight = graphContainer.sizeDelta.y; float yMinimum, yMaximum; CalculateYScale(out yMinimum, out yMaximum);
bool yScaleChanged = yMinimumBefore != yMinimum || yMaximumBefore != yMaximum;
if (!yScaleChanged) { float xPosition = xSize + index * xSize; float yPosition = ((value - yMinimum) / (yMaximum - yMinimum)) * graphHeight;
string tooltipText = getAxisLabelY(value); graphVisualObjectList[index].SetGraphVisualObjectInfo(new Vector2(xPosition, yPosition), xSize, tooltipText); } else { int xIndex = 0; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++) { float xPosition = xSize + xIndex * xSize; float yPosition = ((valueList[i] - yMinimum) / (yMaximum - yMinimum)) * graphHeight;
string tooltipText = getAxisLabelY(valueList[i]); graphVisualObjectList[xIndex].SetGraphVisualObjectInfo(new Vector2(xPosition, yPosition), xSize, tooltipText);
xIndex++; }
for (int i = 0; i < yLabelList.Count; i++) { float normalizedValue = i * 1f / yLabelList.Count; yLabelList[i].GetComponent<Text>().text = getAxisLabelY(yMinimum + (normalizedValue * (yMaximum - yMinimum))); } } }
private void CalculateYScale(out float yMinimum, out float yMaximum) { yMaximum = valueList[0]; yMinimum = valueList[0]; for (int i = Mathf.Max(valueList.Count - maxVisibleValueAmount, 0); i < valueList.Count; i++) { int value = valueList[i]; if (value > yMaximum) { yMaximum = value; } if (value < yMinimum) { yMinimum = value; } }
float yDifference = yMaximum - yMinimum; if (yDifference <= 0) { yDifference = 5f; } yMaximum = yMaximum + (yDifference * 0.2f); yMinimum = yMinimum - (yDifference * 0.2f);
if (startYScaleAtZero) { yMinimum = 0f; } }
private interface IGraphVisual {
IGraphVisualObject CreateGraphVisualObject(Vector2graphPosition, float graphPositionWidth, string tooltipText); void CleanUp();
}
private interface IGraphVisualObject {
void SetGraphVisualObjectInfo(Vector2graphPosition, float graphPositionWidth, string tooltipText); void CleanUp();
}
private class BarChartVisual : IGraphVisual {
private RectTransform graphContainer; private Color barColor; private float barWidthMultiplier;
public BarChartVisual(RectTransform graphContainer, Color barColor, float barWidthMultiplier) { this.graphContainer = graphContainer; this.barColor = barColor; this.barWidthMultiplier = barWidthMultiplier; }
public void CleanUp() { }
public IGraphVisualObject CreateGraphVisualObject(Vector2graphPosition, float graphPositionWidth, string tooltipText) { GameObject barGameObject = CreateBar(graphPosition, graphPositionWidth);
BarChartVisualObject barChartVisualObject = new BarChartVisualObject(barGameObject, barWidthMultiplier); barChartVisualObject.SetGraphVisualObjectInfo(graphPosition, graphPositionWidth, tooltipText);
return barChartVisualObject; }
private GameObject CreateBar(Vector2graphPosition, float barWidth) { GameObject gameObject = new GameObject("bar", typeof(Image)); gameObject.transform.SetParent(graphContainer, false); gameObject.GetComponent<Image>().color = barColor; RectTransform rectTransform = gameObject.GetComponent<RectTransform>(); rectTransform.anchoredPosition = new Vector2(graphPosition.x, 0f); rectTransform.sizeDelta = new Vector2(barWidth * barWidthMultiplier, graphPosition.y); rectTransform.anchorMin = new Vector2(0, 0); rectTransform.anchorMax = new Vector2(0, 0); rectTransform.pivot = new Vector2(.5f, 0f); Button_UI barButtonUI = gameObject.AddComponent<Button_UI>();
return gameObject; }
public class BarChartVisualObject : IGraphVisualObject {
private GameObject barGameObject; private float barWidthMultiplier;
public BarChartVisualObject(GameObject barGameObject, float barWidthMultiplier) { this.barGameObject = barGameObject; this.barWidthMultiplier = barWidthMultiplier; }
public void SetGraphVisualObjectInfo(Vector2graphPosition, float graphPositionWidth, string tooltipText) { RectTransform rectTransform = barGameObject.GetComponent<RectTransform>(); rectTransform.anchoredPosition = new Vector2(graphPosition.x, 0f); rectTransform.sizeDelta = new Vector2(graphPositionWidth * barWidthMultiplier, graphPosition.y);
Button_UI barButtonUI = barGameObject.GetComponent<Button_UI>();
barButtonUI.MouseOverOnceFunc = () => { ShowTooltip_Static(tooltipText, graphPosition); };
barButtonUI.MouseOutOnceFunc = () => { HideTooltip_Static(); }; }
public void CleanUp() { Destroy(barGameObject); } } }
private class LineGraphVisual : IGraphVisual {
private RectTransform graphContainer; private Sprite dotSprite; private LineGraphVisualObject lastLineGraphVisualObject; private Color dotColor; private Color dotConnectionColor;
public LineGraphVisual(RectTransform graphContainer, Sprite dotSprite, Color dotColor, Color dotConnectionColor) { this.graphContainer = graphContainer; this.dotSprite = dotSprite; this.dotColor = dotColor; this.dotConnectionColor = dotConnectionColor; lastLineGraphVisualObject = null; }
public void CleanUp() { lastLineGraphVisualObject = null; }
public IGraphVisualObject CreateGraphVisualObject(Vector2graphPosition, float graphPositionWidth, string tooltipText) { GameObject dotGameObject = CreateDot(graphPosition);
GameObject dotConnectionGameObject = null; if (lastLineGraphVisualObject != null) { dotConnectionGameObject = CreateDotConnection(lastLineGraphVisualObject.GetGraphPosition(), dotGameObject.GetComponent<RectTransform>().anchoredPosition); } LineGraphVisualObject lineGraphVisualObject = new LineGraphVisualObject(dotGameObject, dotConnectionGameObject, lastLineGraphVisualObject); lineGraphVisualObject.SetGraphVisualObjectInfo(graphPosition, graphPositionWidth, tooltipText); lastLineGraphVisualObject = lineGraphVisualObject;
return lineGraphVisualObject; }
private GameObject CreateDot(Vector2 anchoredPosition) { GameObject gameObject = new GameObject("dot", typeof(Image)); gameObject.transform.SetParent(graphContainer, false); gameObject.GetComponent<Image>().sprite = dotSprite; gameObject.GetComponent<Image>().color = dotColor; RectTransform rectTransform = gameObject.GetComponent<RectTransform>(); rectTransform.anchoredPosition = anchoredPosition; rectTransform.sizeDelta = new Vector2(11, 11); rectTransform.anchorMin = new Vector2(0, 0); rectTransform.anchorMax = new Vector2(0, 0); Button_UI dotButtonUI = gameObject.AddComponent<Button_UI>();
return gameObject; }
private GameObject CreateDotConnection(Vector2 dotPositionA, Vector2 dotPositionB) { GameObject gameObject = new GameObject("dotConnection", typeof(Image)); gameObject.transform.SetParent(graphContainer, false); gameObject.GetComponent<Image>().color = dotConnectionColor; gameObject.GetComponent<Image>().raycastTarget = false; RectTransform rectTransform = gameObject.GetComponent<RectTransform>(); Vector2 dir = (dotPositionB - dotPositionA).normalized; float distance = Vector2.Distance(dotPositionA, dotPositionB); rectTransform.anchorMin = new Vector2(0, 0); rectTransform.anchorMax = new Vector2(0, 0); rectTransform.sizeDelta = new Vector2(distance, 3f); rectTransform.anchoredPosition = dotPositionA + dir * distance * .5f; rectTransform.localEulerAngles = new Vector3(0, 0, UtilsClass.GetAngleFromVectorFloat(dir)); return gameObject; }
public class LineGraphVisualObject : IGraphVisualObject {
public event EventHandler OnChangedGraphVisualObjectInfo;
private GameObject dotGameObject; private GameObject dotConnectionGameObject; private LineGraphVisualObject lastVisualObject;
public LineGraphVisualObject(GameObject dotGameObject, GameObject dotConnectionGameObject, LineGraphVisualObject lastVisualObject) { this.dotGameObject = dotGameObject; this.dotConnectionGameObject = dotConnectionGameObject; this.lastVisualObject = lastVisualObject;
if (lastVisualObject != null) { lastVisualObject.OnChangedGraphVisualObjectInfo += LastVisualObject_OnChangedGraphVisualObjectInfo; } }
private void LastVisualObject_OnChangedGraphVisualObjectInfo(object sender, EventArgs e) { UpdateDotConnection(); }
public void SetGraphVisualObjectInfo(Vector2graphPosition, float graphPositionWidth, string tooltipText) { RectTransform rectTransform = dotGameObject.GetComponent<RectTransform>(); rectTransform.anchoredPosition = graphPosition;
UpdateDotConnection();
Button_UI dotButtonUI = dotGameObject.GetComponent<Button_UI>();
dotButtonUI.MouseOverOnceFunc = () => { ShowTooltip_Static(tooltipText, graphPosition); }; dotButtonUI.MouseOutOnceFunc = () => { HideTooltip_Static(); };
if (OnChangedGraphVisualObjectInfo != null) OnChangedGraphVisualObjectInfo(this, EventArgs.Empty); }
public void CleanUp() { Destroy(dotGameObject); Destroy(dotConnectionGameObject); }
public Vector2 GetGraphPosition() { RectTransform rectTransform = dotGameObject.GetComponent<RectTransform>(); return rectTransform.anchoredPosition; }
private void UpdateDotConnection() { if (dotConnectionGameObject != null) { RectTransform dotConnectionRectTransform = dotConnectionGameObject.GetComponent<RectTransform>(); Vector2 dir = (lastVisualObject.GetGraphPosition() - GetGraphPosition()).normalized; float distance = Vector2.Distance(GetGraphPosition(), lastVisualObject.GetGraphPosition()); dotConnectionRectTransform.sizeDelta = new Vector2(distance, 3f); dotConnectionRectTransform.anchoredPosition = GetGraphPosition() + dir * distance * .5f; dotConnectionRectTransform.localEulerAngles = new Vector3(0, 0, UtilsClass.GetAngleFromVectorFloat(dir)); } } } } }
|